﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TeleRad.Entities;
using TeleRad.Repository;
using TeleRad.Models;
using System.Security.Principal;
using System.IO;
using System.Web.Hosting;


namespace TeleRad.Services
{
    public class ContentService : TeleRad.Services.IContentService
    {
        private readonly IRepository<PatientInfo> _PatientInfo_Repository;
        private readonly IPrincipal _User;

        public ContentService(IRepository<PatientInfo> PatientInfo_Repository, IPrincipal User)
        {
            this._PatientInfo_Repository = PatientInfo_Repository;
            this._User = User;
        }

        public PatientInfo GetPatientBy(string UserName)
        {
            return _PatientInfo_Repository.Table.Where(p => p.UserName == UserName).SingleOrDefault();
        }

        public PatientProfile GetProfile()
        {
            var Patient = GetPatientBy(_User.Identity.Name);
            return new PatientProfile()
            {
                PatientName = Patient.Name,
                Age = Patient.Age,
                Sex = Patient.Age,
                Number_Of_View = Patient.ViewsNumber
            };
        }

        public string[] GetImagesPath()
        {

            return GetResourcePaths(TeleRad.Properties.Resources.ImageExtension, "image");
        }

        public string[] GetReportsPath()
        {
            return GetResourcePaths(TeleRad.Properties.Resources.ReportExtension, "application");
        }

        public string[] GetVideosPath()
        {
            return GetResourcePaths(TeleRad.Properties.Resources.VideoExtension, "video");
        }

        public FileStream GetRequestedFile(int CompanyId, int PatientId, string FileName)
        {
            var Patient = GetPatientBy(_User.Identity.Name);
            if (Patient.Id == PatientId && Patient.CompanyCode == CompanyId)
            {
                string resource_path = String.Format(TeleRad.Properties.Resources.TrueDataDir, CompanyId, PatientId, FileName);
                return new FileStream(resource_path,FileMode.Open);
            }
            else
                return null;
        }

        private string[] GetResourcePaths(string FileExtensions,string ContentType)
        {
            var Patient_Info = _PatientInfo_Repository.Table.Where(p => p.UserName == _User.Identity.Name).SingleOrDefault();
            // need File System Access Seperation.
            string DirectoryPath = String.Format(TeleRad.Properties.Resources.ServerDataDir, Patient_Info.CompanyCode, Patient_Info.Id);
            DirectoryInfo Directory_info = new DirectoryInfo(DirectoryPath);
            FileInfo[] files_info = FileExtensions.Split('|')
                                                  .SelectMany(ext => Directory_info.GetFiles(ext, SearchOption.TopDirectoryOnly))
                                                  .Distinct()
                                                  .ToArray();

            string[] Paths = new string[files_info.Length];
            for (int i = 0; i < files_info.Length; i++)
            {
                Paths[i] = String.Format(TeleRad.Properties.Resources.ClientDataDir, Patient_Info.CompanyCode, Patient_Info.Id, files_info[i].Name, ContentType + files_info[i].Extension.Replace('.', '/'));
            }

            return Paths;
        }

    }
}